﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace Provausio.Common.Web
{
    public enum HttpRequestMethod
    {
        Get,
        Post,
        Put,
        Delete
    }

    public class SimpleWebResponse
    {
        public string Method { get; set; }
        public string RequestUrl { get; set; }
        public string OriginalRequest { get; private set; }
        public int StatusCode { get; set; }
        public string Body { get; set; }
        public string ContentType { get; private set; }
        public WebHeaderCollection Headers { get; set; }

        #region Constructors
        public SimpleWebResponse(string url, HttpRequestMethod method, string contentType = "application/x-www-form-urlencoded")
        {
            RequestUrl = url;
            Method = method.ToString();
            ContentType = contentType;
        }

        public SimpleWebResponse(string url, string queryString, HttpRequestMethod method, string contentType = "application/x-www-form-urlencoded")
        {
            RequestUrl = url;
            OriginalRequest = queryString;
            Method = method.ToString();
            ContentType = contentType;
        }

        public SimpleWebResponse(string url, object payLoad, HttpRequestMethod method, string contentType = "application/x-www-form-urlencoded")
        {
            throw new NotImplementedException();
        }

        public SimpleWebResponse(string url, object[] payLoad, HttpRequestMethod method, string contentType = "application/x-www-form-urlencoded")
        {
            throw new NotImplementedException();
        }

        public SimpleWebResponse(string url, object[] payLoad, string queryString, HttpRequestMethod method, string contentType = "application/x-www-form-urlencoded")
        {
            throw new NotImplementedException();
        }
        #endregion

        public SimpleWebResponse GetReponse()
        {
            SetProperties(GetRequest());
            return this;
        }

        private HttpWebRequest GetRequest()
        {
            var req = (HttpWebRequest)WebRequest.Create(RequestUrl);
            req.Method = Method;
            req.ContentType = ContentType;

            //build the fields stream
            var fields = Encoding.UTF8.GetBytes(OriginalRequest);

            //TODO build payload streams?

            req.ContentLength = fields.Length;

            //put the request info into the stream
            var requestStream = req.GetRequestStream();
            requestStream.Write(fields, 0, fields.Length);
            requestStream.Close();

            return req;
        }

        private void SetProperties(WebRequest req)
        {
            var resp = (HttpWebResponse)req.GetResponse();

            StatusCode = (int)resp.StatusCode;
            Headers = resp.Headers;
            using (var rdr = new StreamReader(resp.GetResponseStream()))
            {
                Body = rdr.ReadToEnd();
            }
        }
    }
}